# -*- encoding:utf8 -*-
from attendance.models import AttendRecord,AttendReport
from staff.models import StaffInfo
from django.utils import timezone
from attendance.convert import date2str,str2date
from attendance.tool_func import get_or_none
from workday import is_workday

def attend_record(start_date=None,end_date=None,keyword=None,workday=None,restday=None):
    """
    staff:用于验证是否有权限,例如Hr
    """
    
    dc={}
    if start_date and end_date:
        dc['date__gte']=start_date
        dc['date__lte']=end_date
    else:
        now=timezone.now().date()
        firstday=date2str(now.replace(day=1))
        nowstr=date2str(now)       
        dc['date__gte']=firstday
        dc['date__lte']=nowstr 
    if keyword:
        if keyword.startswith('AE'):
            staff=get_or_none(StaffInfo,employee_id=keyword)
            if staff:
                dc['staff']=staff
        else:
            sl=[]
            for staff in StaffInfo.objects.filter(cn_name__icontains=keyword)|StaffInfo.objects.filter(en_name__icontains=keyword):
                sl.append(staff)
            dc['staff__in']=sl
    
    outlist=[]
    for row in AttendRecord.objects.filter(**dc):
        out=False
        row_date_obj=str2date( row.date) 
        if workday:
            iswork,span=is_workday(row_date_obj)
            if iswork:
                out=True
        if restday:
            iswork,span=is_workday(row_date_obj)
            if not iswork:
                out=True
        if not out:
            continue
        empid=row.staff.employee_id
        name=row.staff.cn_name
        outlist.append( {'empid':empid,
                    'name':name,
                    'date':row.date,
                    'enter_time':row.enter_time,
                    'leave_time':row.leave_time,
                    'late':row.late_level,
                    'late_team':row.late_team,
                    'late_person':row.late_person,
                    'workspan':row.workspan,
                    'overtime':row.overtime,
                    'absent':row.absent,
                   })
    
    return outlist


def attend_report(start_date=None,due_date=None,keyword=None):
    """以字典的方式，返回某段日期内，某人的考勤统计。
    Args:
    keyword:empid或者中文姓名。
    
    """
    dc={}
    if start_date is None or due_date is None:
        end=start_date if start_date else due_date
        if end is None:
            end=date2str(timezone.now().date())
        start=date2str( str2date(end).replace(day=1))
    else:
        start=start_date
        end=due_date
    # dc['start_date']
    # if start_date and end_date:
        # dc['date__gte']=start_date
        # dc['date__lte']=end_date
    # else:
        # now=timezone.now().date()
        # firstday=date2str(now.replace(day=1))
        # nowstr=date2str(now)       
        # dc['date__gte']=firstday
        # dc['date__lte']=nowstr 
    if keyword:
        if keyword.startswith('AE'):
            staff=get_or_none(StaffInfo,employee_id=keyword)
            if staff:
                dc['staff']=staff
        else:
            sl=[]
            for staff in StaffInfo.objects.filter(cn_name__icontains=keyword)|StaffInfo.objects.filter(en_name__icontains=keyword):
                sl.append(staff)
            dc['staff__in']=sl 
    out =[]
    for row in AttendReport.objects.filter(start_date__lte=end,start_date__gte=start,**dc)|\
        AttendReport.objects.filter(due_date__lte=end,due_date__gte=start,**dc):
        out.append({
            'empid':row.staff.employee_id,
            'name':row.staff.cn_name,
            'start_date':row.start_date,
            'due_date':row.due_date,
            'person_leave':row.person_leave,
            'sick_leave':row.sick,
            'annual_leave':row.annual,
            'other_paid_leave':row.other_paid_leave,
            'swap_off':row.swap_off,
            'paid_leave':row.paid_leave,
            'late1':row.late1,
            'late2':row.late2,
            'late3':row.late3,
            'late4':row.late4,
            'late_person':row.late_person,
            'late_team':row.late_team,
            'overtime':row.overtime,
            'exp_workday':row.exp_workday,
            'act_workday':row.act_workday,
            'allow_day':row.allow_day,
            'leave_day':row.leave_day,
            'deduction':row.deduction_day,
            'absent':row.absent,
            'early_leave':row.early_leave,
            'full_attend':row.full_attend,
            'process':row.process,
            'note':row.note
        })
        
    return out

            